﻿using PortalLogReport.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PortalLogReport.Models;
using PortalLogReport.Common;

namespace PortalLogReport.Controllers
{
    public class ReportController : Controller
    {
        #region Fields
        IResultService _resultService;
        #endregion

        #region Constructors

        public ReportController()
            : this(new ResultService())
        {
        }

        public ReportController(IResultService resultService)
        {
            _resultService = resultService;
        }

        #endregion

        public ActionResult Index()
        {
            _resultService.CreateRepository(ChartCommon.DimensionTypes.Category.ToString(), new KeyValuePair<string, string>("All", "All"), ChartCommon.Interval.None.ToString());
            var data = _resultService.GetResult(DateTime.Now, DateTime.Now);

            List<BarChartViewModel> result = new List<BarChartViewModel>();
            foreach (var item in data.BaseModel.Keys)
            {
                result.Add(BarChartViewModel.ConvertToViewModel(item, data.BaseModel[item]));
            }
            ViewBag.DataSet = result;

            return View();
        }


        public ActionResult Dashboard()
        {
            List<SelectListItem> ltInterval = new List<SelectListItem>();
            ltInterval.Add(new SelectListItem { Text = "Month", Value = "Month", Selected = true });
            ltInterval.Add(new SelectListItem { Text = "Week", Value = "Week" ,Selected = false});
            ltInterval.Add(new SelectListItem { Text = "Year", Value = "Year", Selected = false });
            ViewData["Interval"] = ltInterval;

            List<SelectListItem> ltLevel1 = new List<SelectListItem>();
            ltLevel1.Add(new SelectListItem { Text = "appdomainname", Value = "Category" });
            ltLevel1.Add(new SelectListItem { Text = "message", Value = "Error" });
            ViewData["Level1"] = ltLevel1;
            
            return View();
        }


        public JsonResult GetGroupByData(string chartType, string type, string startDate, string endDate, string interval = "Month")
        {
            var data = GetBaseChartModelData(interval, type, "All", startDate, endDate);

            List<BarChartViewModel> result = new List<BarChartViewModel>();
            foreach (var item in data.BaseModel.Keys)
            {
                result.Add(BarChartViewModel.ConvertToViewModel(item, data.BaseModel[item]));
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSubCategoryData(string chartType, string type, string category, string startDate, string endDate, string subCategory = "All", string interval = "None")
        {
            if (string.IsNullOrEmpty(endDate))
            {
                endDate = GetEndDate(startDate, interval).ToString();
            }

            var data = GetBaseChartModelData(interval, type, category, startDate, endDate, subCategory);

            List<PieChartViewModel> result = new List<PieChartViewModel>();
            if (data != null && data.BaseModel != null)
            {
                var enumerator = data.BaseModel.GetEnumerator();
                if (enumerator.MoveNext())
                {
                    var viewModel = PieChartViewModel.ConvertToViewModel(category, enumerator.Current.Value);

                    viewModel.LabelPosition = LabelPosition.inside;
                    viewModel.Radius = "[0,100]";
                    result.Add(viewModel);
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private DateTime GetEndDate(string startDate, string interval)
        {
            DateTime dtEndDate;
            if (DateTime.TryParse(startDate, out dtEndDate))
            {
                var intervalByEnum = ChartCommon.Interval.None;
                if (Enum.TryParse<ChartCommon.Interval>(interval, out intervalByEnum))
                {
                    switch (intervalByEnum)
                    {
                        case ChartCommon.Interval.Week:
                            dtEndDate = dtEndDate.AddDays(7);
                            break;
                        case ChartCommon.Interval.Month:
                            dtEndDate = dtEndDate.AddMonths(1);
                            break;
                        case ChartCommon.Interval.Quarter:
                            dtEndDate = dtEndDate.AddMonths(3);
                            break;
                        case ChartCommon.Interval.Year:
                            dtEndDate = dtEndDate.AddYears(1);
                            break;
                        default:
                            dtEndDate = dtEndDate.AddMonths(1);
                            break;
                    }

                }
                dtEndDate = new DateTime(dtEndDate.Year, dtEndDate.Month, 1);
            }
            return dtEndDate;
        }

        public BaseChartModel GetBaseChartModelData(string interval, string dimensionType, string category, string startDate, string endDate, string subCategory = "All")
        {
            DateTime dtStartDate, dtEndDate;
            DateTime.TryParse(startDate, out dtStartDate);
            DateTime.TryParse(endDate, out dtEndDate);

            if (dtStartDate == DateTime.MinValue || dtEndDate == DateTime.MinValue)
            {
                dtStartDate = DateTime.Now;
                dtEndDate = DateTime.Now;
            }

            _resultService.CreateRepository(dimensionType, new KeyValuePair<string, string>(category, subCategory), interval);

            var data = _resultService.GetResult(dtStartDate, dtEndDate);

            return data;

        }


        public JsonResult GetOrderByData(string chartType, string dimensionType, string startDate, string endDate, string interval = "None")
        {
            var data = GetBaseChartModelData(interval, dimensionType, "All", startDate, endDate);

            List<BarChartViewModel> result = new List<BarChartViewModel>();

            if (data != null && data.BaseModel != null)
            {
                var enumerator = data.BaseModel.GetEnumerator();
                if (enumerator.MoveNext())
                {
                    result.Add(BarChartViewModel.ConvertToViewModel(enumerator.Current.Key, enumerator.Current.Value));
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public string KeyValuePair { get; set; }
    }
}

